﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 剧情设计器2._0
{
    public partial class VisualView : Form
    {

        int offset_x = 0, offset_y = 0;
        bool mouseDown = false;
        int start_x, start_y;
        public VisualView()
        {
            InitializeComponent();
        }



        private void stage_Paint(object sender, PaintEventArgs e)
        {
            PictureBox mem = new PictureBox() { Width = stage.Width, Height = stage.Height };

            Graphics g = e.Graphics;
            //Graphics g = mem.CreateGraphics();
            Pen pen = new Pen(Color.Black);
            g.Clear(Color.White);
            g.DrawLine(pen, 0, offset_y + 12, stage.Width, offset_y + 12);
            for(int i = 0; i < Common.story.getTime(); i++)
            {
                g.DrawString(i.ToString(), new Font("微软雅黑", 10), pen.Brush, offset_x + i * (112 + 8) + 56, offset_y + 8 - 12);
            }
            Common.story.draw(g, offset_x, offset_y + 12);
            stage.Image = mem.Image;
            //stage.Refresh();
        }

        private void VisualView_Load(object sender, EventArgs e)
        {
            stage.Width = Common.story.getW() + offset_x + 10;
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
        }

        private void stage_MouseDown(object sender, MouseEventArgs e)
        {
            mouseDown = true;
            start_x = e.X;
            start_y = e.Y;
        }

        private void stage_MouseUp(object sender, MouseEventArgs e)
        {
            mouseDown = false;
        }

        private void VisualView_FormClosed(object sender, FormClosedEventArgs e)
        {
            Common.VisualViewOpened = false;
        }

        private void stage_MouseMove(object sender, MouseEventArgs e)
        {
            if(mouseDown)
            {
                int dx = e.X - start_x;
                int dy = e.Y - start_y;
                offset_x += dx;
                offset_y += dy;
                start_x = e.X;
                start_y = e.Y;
                //stage_Paint(sender, new PaintEventArgs(stage.CreateGraphics(), new Rectangle(0, 0, stage.Width, stage.Height)));
            }
        }

        private void timer_paint_Tick(object sender, EventArgs e)
        {
            stage_Paint(sender, new PaintEventArgs(stage.CreateGraphics(), new Rectangle(0, 0, stage.Width, stage.Height)));
        }
    }
}
